package com.yitao.cms.service.product;


import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.constant.ProductConstant;
import com.yitao.client.dto.product.ProductImgDto;
import com.yitao.client.model.product.ProductImgModel;
import com.yitao.client.model.product.ProductInfoModel;
import com.yitao.client.model.product.QProductImgModel;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import com.yitao.cms.utils.BaseUtils;
import com.yitao.cms.vo.product.BaseImgDto;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ProductImgService extends GenericEntityService<ProductImgModel, ProductImgDto, QProductImgModel> {

    private BaseUtils base = BaseUtils.getInstance();
    public ProductImgService(EntityManager entityManager) {
        super(entityManager);
    }

    @Transactional
    public ProductImgDto save(ProductImgDto productImgDto){
        return convertEntityToDto(repository.save(convertDtoToEntity(productImgDto)));
    }

    @Transactional
    public ProductImgModel save(ProductImgModel productImgModel){
        return repository.save(productImgModel);
    }

    /**
     * 根据参数添加图片数据
     * @param productImgType
     * @param productInfoModel
     * @param userSession
     * @return
     */
    @Transactional
    public ProductImgModel insertProductImage(ProductConstant.ImgTypeEnum productImgType, ProductInfoModel productInfoModel,
                                              ProductImgModel productImgModel, OperationUserCmsModel userSession ){
        productImgModel.setImageType(productImgType.getCode());
        productImgModel.setProductId(Long.valueOf(productInfoModel.getId()));
        productImgModel.setProductCode(productInfoModel.getProductCode());
        productImgModel.setCreateBy(Long.valueOf(userSession.getId()));
        productImgModel.setCreateTime(base.getNow());
        productImgModel.setDelete(false);
        productImgModel.setCreateTime(productInfoModel.getCreateTime());
        productImgModel.setLastUpdateBy(Long.valueOf(userSession.getId()));
        productImgModel.setLastUpdateTime(base.getNow());
        return repository.save(productImgModel);
    }

    @Transactional
    public void updateProductImage(ProductConstant.ImgTypeEnum productImgType,ProductInfoModel productInfoModel){
        //默认把之前的借口默认为删除
        repository.updateByWhere(Q.productId.eq(productInfoModel.getId())
                .and(Q.imageType.eq(productImgType.getCode()))
                .and(Q.skuId.isNull()),it ->
                it.set(Q.isDelete,true));
    }

    /**
     * 根据skuId查询数据信息
     * @param SkuId
     * @return
     */
    public ProductImgModel findImgBySkuId(Long SkuId){
        if (SkuId!=null){
          List<ProductImgModel> productImgModels = repository.findAll(Q.skuId.eq(SkuId).and(Q.isDelete.eq(false)));
          if (productImgModels.size()>0){
              return productImgModels.get(0);
          }
        }
        return null;
    }


    /**
     * 根据productId删除SKU图片信息
     * @param productId
     * @return
     */
    @Transactional
    public DataOutput deleteProductSKUImgByProductId(Long productId){
        try {
            if (productId!=null){
                int result = repository.updateByWhere(Q.productId.eq(productId)
                                .and(Q.skuId.isNotNull())
                                .and(Q.imageType.eq(ProductConstant.ImgTypeEnum.SKU.getCode()))
                        ,it ->it.set(Q.isDelete,true));
                return new DataOutput(CommonStatus.SUCCESS);
            }else{
                return new DataOutput(CommonStatus.BAD_REQUEST);
            }
        } catch (Exception e) {
            logger.error("根据productId:{},删除SKU图片失败:{}",productId,e.getMessage());
            return new DataOutput(CommonStatus.SERVER_ERROR);
        }
    }

    /**
     * 根据产品ID及图片类型查询图片
     * @param productId
     * @param imgTypeEnum
     * @return
     */
    public  List<BaseImgDto> findImageByProductIdAndType(Long productId,ProductConstant.ImgTypeEnum imgTypeEnum){
        if (productId!=null){
            List<BaseImgDto> baseImgDtos= repository.findAll(Q.productId.eq(productId)
                    .and(Q.isDelete.eq(false))
                    .and(Q.imageType.eq(imgTypeEnum.getCode())),new Sort(Sort.Direction.ASC,"imageSort","id"))
                    .stream().map(it -> {
                        BaseImgDto baseImgDto = new BaseImgDto();
                        BeanUtils.copyProperties(it,baseImgDto);
                        return baseImgDto;
                    }).collect(Collectors.toList());
            return baseImgDtos;
        }
        return null;


    }

}
